<?php
namespace app\mobile\controller;
use app\common\controller\Base;
use think\Db;
use app\index\model\Zaixian;
use app\index\model\Username;
use thinkcms\auth\Auth;
use think\Cookie;
use GuzzleHttp\json_encode;

class Pocessing extends Base
{
	public function login(){
		$username = input('post.username') ? input('post.username') : '';
		$password = input('post.password') ? input('post.password') : '';
		$code = input('post.code') ? input('post.code') : '';
		if($username && $password && $code) {
			$password = md5($password);
			//查询会员信息
			$result = Db::name('username')->where(['username'=>$username,'password'=>$password])->find();
			if($result['open'] == 1) {
				return 3;
			}
			if($result['sales'] == 1) {
				return 9;
			}
			//代理是否登录
/* 			if($result['dl']) {
				return 2;
			} */
			if(!captcha_check($code)) {
			    return 4;
			}
			//处理会员登录
			if($result['id'] > 0) {
				cookie('username',$username);
				cookie('password',$password);
				$login_key = suiji_zifu(15);
				cookie('login_key',$login_key);
				$login_time = time();
				$login_ip = get_ip();
				$zx_list = Db::name('zaixian')->where('username',$username)->find();
				$login_url = $_SERVER['HTTP_HOST'];
				$zaixian = new Zaixian();
				if($zx_list['id'] > 0) {
					//更新用户在线信息
					$zaixian->updateOnlineTime($zx_list['id'],$username,$login_time,0,$login_ip,$login_url);
				} else {
					//插入在线表
					$zaixian->setOnlineTime($username,$login_time,0,$login_ip,$login_url);
				}
				//会员等级自动提升
				$us_class = Db::name('us_class')->field('id,czl,xml')->select();
				$usclass = $result['us_class'];
				foreach ($us_class as $v) {
				    if($v['czl'] <= $result['czzje']) {
				        if($v['xml'] <= $result['tzzje']) {
				            $usclass = $v['id'];
				        }
				    }
				}
				//更新用户登录时间信息
				$user = new Username();
				$user->updateLoginInfo($result['id'],$login_key,$login_time,$usclass,$login_ip);
				//记录日志
				$this->caozuoRizhiJilu('2',admin_text(49),$login_ip,$username,2);
				//更新代理下线人数
				$user = Db::name('username')->where(['nbzh'=>0,'swzh'=>0,'open'=>0,'dl'=>1])->select();
				foreach ($user as $val){
				    $user_dl = Db::name('username')->where('tjr',$val['username'])->where('tjrid',$val['id'])->count();
				    $data['xjrens'] = $user_dl;
				    Db::name('username')->where('id',$val['id'])->update($data);
				}
				echo 1;
			} else {
				echo 2;
			}
		} else {
			echo 2;
		}
	}
	public function shiwan(){
		//判断试玩功能是否开启
		$swsm = Db::name('xt_shiwan')->find();
		if($swsm['open'] == 1) {
			$username = $swsm['qianzhui'];
			$yuscount = '';
			for($t=1;$t<=$swsm['swcd'];$t++) {
				$yuscount .= rand(0,9);
			}
			$username = $username.$yuscount;
			$password = md5('123456');
			$login_key = suiji_zifu(15);
			//获得IP并解析地址
			 $login_ip = $this->login_ip;
			/*$ip = @file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$login_ip}");
			$position = ''; 
			if($ip) {
				$ip = json_decode($ip,true);
				if( -1!=$ip['ret'] ) $position = $ip['country'].' '.$ip['province'].' '.$ip['city'];
			}
			*/
			//写入账号
			$user = new Username();
			$user->setTryPlayUser($username,$password,$swsm['je'],'0','试玩',time(),$login_ip,'','1','1',$login_key,'0',2);
			//设置用户cookie
			cookie("username",$username);
			cookie('password',$password);
			cookie("login_key",$login_key);
			if(computer_or_mobile() == 'mobile') {
				echo 'sw';
				//js_tishi('申请成功！','/mobile/Bet/index');
			}
		} else {
			echo 'wkq';
			//js_tishi('系统未开启试玩功能！','/mobile/Bet/index');
		}
		
	}
	public function logout(){
		Cookie::delete('username');
		Cookie::delete('password');
		Cookie::delete('login_key');
		cookie(null);
		$this->redirect('index/mhome');
	}
	public function setCookieCaizhong(){
		$wanfa_tzhm = input('tzhm') ? input('tzhm') : '';
		$timestamp = input('timestamp');
		$digit = input('digit');
		cookie('wanfa_tzhm_'.$timestamp,$wanfa_tzhm);
		cookie('wanfa_digit_'.$timestamp,$digit);
		$touzhu =array();
		foreach ($_COOKIE as $key=>$value){
			if(strstr($key, 'wanfa_tzhm_')!==false){
				$touzhu[]=$value;
			}
		}
		return json($touzhu);
	}
	public function setArrayCookie(){
		$tzhm = input('tzhm')?input('tzhm'):'';
		$timestamp = input('timestamp');
		$cishu = explode('[H]', $tzhm);
		$time = explode('[H]',$timestamp);

		foreach ($cishu as $key=>$value){
			foreach ($time as $vk=>$val){
				if($key==$vk){
					cookie('wanfa_tzhm_'.$val,$value);
				}
			}
		}
		
		$touzhu =array();
		foreach ($_COOKIE as $key=>$value){
			if(strstr($key, 'wanfa_tzhm_')!==false){
				$touzhu[]=$value;
			}
		}
		return json_encode($touzhu);
	}
	public function sccookie(){
		Cookie::clear('wanfa_tzhm_');
		Cookie::clear('wanfa_digit_');
		return true;
	}
	public function delcookie(){
		$zdid = input('zdid') ? input('zdid') : '';
		if($zdid){
			Cookie::delete('wanfa_tzhm_'.$zdid);
			Cookie::delete('wanfa_digit_'.$zdid);
			return true;
		}else{
			echo 'NO';
		}
	}
	
}
